/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treeship.core.ship.space.impl;

import treeship.core.ship.space.Distance;
import treeship.core.ship.space.Position;
import treeship.core.ship.space.Speed;

/**
 *
 * @author moonchild
 */
public class Position2D extends Position {

    private double x;
    private double y;

    public Position2D(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public Distance distanceFrom(Position p1) {
        Position2D pp1 = (Position2D) p1;
        double xd = x - pp1.x;
        double yd = y - pp1.y;
        double d = Math.sqrt(xd * xd + yd * yd);
        return new Distance(d);
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    @Override
    @Deprecated
    public double getZ() {
        return 0;
    }

    @Override
    public String toString() {
        return "[" + Math.round(getX() * 100) / 100.0 + "|" + Math.round(getY() * 100) / 100.0 + "]";
    }

    @Override
    public Position goFromPositionWithSpeed(Position fromPosition, Speed speed) {
        Position2D fromP2 = (Position2D) fromPosition;
        Distance distance = distanceFrom(fromP2);
        if (distance.getValue() <= speed.getValue()) {
            return fromP2;
        }
        double x1=fromPosition.getX();
        double y1=fromPosition.getY();
        
        double x2=x;
        double y2=y;

        double newX=x1+((x2-x1)*speed.getValue()/distance.getValue());
        double newY=y1+((y2-y1)*speed.getValue()/distance.getValue());

        return new Position2D(newX, newY);

    }
}
